<?php

namespace App\Http\Requests\Admin\Customer\BrandSeries;

use App\Exceptions\ApiErrCode;
use App\Exceptions\ApiException;
use App\Http\Requests\ApiRequest;
use App\Models\Panx\Brand;
use App\Services\Utils\Guard;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;

/**
 * @property string name
 * @property string sort
 * @property string brand_id
 * @property UploadedFile|string image
 */
class BrandSeriesStoreRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $customer_id = Guard::customerAdmin()->customer_id;
        $brand_id = $this->brand_id;

        $image_rule = is_file($this->image) ? 'required|file' : 'required|string';

        $data = [
            'name'     => [
                'required', 'string', 'max:' . self::MAX_STRING,
                Rule::unique('brand_series', 'name')
                    // 同一客户不同品牌下品牌系列应可以重名
                    ->where($this->closureWhere(compact('customer_id', 'brand_id')))
            ],
            'sort'     => 'sometimes|required|int|between:0,10000',
            'brand_id' => 'required|int|min:1',
            'image'    => $image_rule,
        ];

        return $data;
    }
}
